package com.qfhd.soft.cjzcore.Core;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

import java.util.ArrayList;
import java.util.List;

public class CJStorage {

    public static String UserInfo_MapName           = CJCrypto.GetStringMd5("userInfo");
    public static String UserInfo_Key_PhoneNumber   = CJCrypto.GetStringMd5("phoneNumber");
    public static String UserInfo_Key_Token         = CJCrypto.GetStringMd5("token");
    public static String UserInfo_Key_UserInfo      = CJCrypto.GetStringMd5("userInfo");
    public static String UserInfo_Key_IsLogin       = CJCrypto.GetStringMd5("isLogin");
    public static String UserInfo_Key_IsOnVoice     = CJCrypto.GetStringMd5("isOnVoice");
    public static String UserInfo_Key_IsShowYingsi  = CJCrypto.GetStringMd5("IsShowYingsi");

    public static String USED_AD_MapName            = CJCrypto.GetStringMd5("USED_AD_MapName");
    public static String USED_AD_Value              = CJCrypto.GetStringMd5("USED_AD_Value");

    public static String SCRATCH_MapName            = CJCrypto.GetStringMd5("SCRATCH_MapName");
    public static String SCRATCH_KEY_COUNT          = CJCrypto.GetStringMd5("SCRATCH_KEY_COUNT");

    private static CJStorage qfStorage = null;

    public static CJStorage getInstance() {
        if (qfStorage == null) {
            synchronized (CJStorage.class) {
                if (qfStorage == null) {
                    qfStorage = new CJStorage();
                }
            }
        }
        return qfStorage;
    }

    public void pushStringData(Context context, String mapName, String key, String value) {
        SharedPreferences mySharedPreferences = context.getSharedPreferences(mapName, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = mySharedPreferences.edit();
        editor.putString(key, value);
        editor.apply();
    }

    public void pushIntData(Context context, String mapName, String key, int value) {
        SharedPreferences mySharedPreferences = context.getSharedPreferences(mapName, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = mySharedPreferences.edit();
        editor.putInt(key, value);
        editor.apply();
    }

    public void pushLongData(Context context, String mapName, String key, long value) {
        SharedPreferences mySharedPreferences = context.getSharedPreferences(mapName, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = mySharedPreferences.edit();
        editor.putLong(key, value);
        editor.apply();
    }

    public void clearMap(Context context, String mapName) {
        SharedPreferences mySharedPreferences = context.getSharedPreferences(mapName, Activity.MODE_PRIVATE);
        if (mySharedPreferences != null) {
            SharedPreferences.Editor editor = mySharedPreferences.edit();
            editor.clear();
            editor.apply();
        }
    }

    public boolean hasKey(Context context, String mapName, String key) {
        SharedPreferences mySharedPreferences = context.getSharedPreferences(mapName, Activity.MODE_PRIVATE);
        if (mySharedPreferences != null) {
            if (mySharedPreferences.contains(key)) {
                return true;
            }
        }
        return false;
    }

    public void removeKey(Context context, String mapName, String key) {
        SharedPreferences mySharedPreferences = context.getSharedPreferences(mapName, Activity.MODE_PRIVATE);
        if (mySharedPreferences != null) {
            if (mySharedPreferences.contains(key)) {
                SharedPreferences.Editor editor = mySharedPreferences.edit();
                editor.remove(key);
                editor.apply();
            }
        }
    }

    public String getStringData(Context context, String mapName, String key) {
        SharedPreferences mySharedPreferences = context.getSharedPreferences(mapName, Activity.MODE_PRIVATE);
        if (mySharedPreferences != null) {
            String value = "";
            if (mySharedPreferences.contains(key)) {
                value = mySharedPreferences.getString(key, "");
            }
            return value;
        }
        return "";
    }

    public int getIntData(Context context, String mapName, String key) {
        SharedPreferences mySharedPreferences = context.getSharedPreferences(mapName, Activity.MODE_PRIVATE);
        if (mySharedPreferences != null) {
            int value = 0;
            if (mySharedPreferences.contains(key)) {
                value = mySharedPreferences.getInt(key, 0);
            }
            return value;
        }
        return 0;
    }

    public long getLongData(Context context, String mapName, String key) {
        SharedPreferences mySharedPreferences = context.getSharedPreferences(mapName, Activity.MODE_PRIVATE);
        if (mySharedPreferences != null) {
            long value = 0;
            if (mySharedPreferences.contains(key)) {
                value = mySharedPreferences.getLong(key, 0);
            }
            return value;
        }
        return 0;
    }

    public void pushBooleanData(Context context, String mapName, String key, boolean value) {
        SharedPreferences mySharedPreferences = context.getSharedPreferences(mapName, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = mySharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    public boolean getBooleanData(Context context, String mapName, String key) {
        SharedPreferences mySharedPreferences = context.getSharedPreferences(mapName, Activity.MODE_PRIVATE);
        if (mySharedPreferences != null) {
            boolean value = false;
            if (mySharedPreferences.contains(key)) {
                value = mySharedPreferences.getBoolean(key, false);
            }
            return value;
        }
        return false;
    }

    public int getKeysCount(Context context, String mapName) {
        SharedPreferences mySharedPreferences = context.getSharedPreferences(mapName, Activity.MODE_PRIVATE);
        if (mySharedPreferences != null) {
            return mySharedPreferences.getAll().size();
        }
        return 0;
    }

    public List<String> getMapKeys(Context context, String mapName) {
        SharedPreferences mySharedPreferences = context.getSharedPreferences(mapName, Activity.MODE_PRIVATE);
        if (mySharedPreferences != null) {
            return new ArrayList<>(mySharedPreferences.getAll().keySet());
        }
        return null;
    }
}
